package gamble.money.email;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class SendMailImp implements SendMail {
	
	private String email;
	private String ranNo;
	public SendMailImp() {
	}
	
	public SendMailImp(String email, String ranNo) {
		this.email = email;
		this.ranNo =  ranNo;
	}
	
	private Boolean sendEmail(String from, String to , String subject, String content){
		
		Properties props = new Properties();
		//---------smtp setting Start
		String host = "smtp.gmail.com";
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.transport.protocol", "smtp");
		props.put("mail.smtp.host", host);
		props.put("mail.smtp.port", "25");
		props.put("mail.smtp.auth", "true");
		//---------smtp setting End
		
		Authenticator auth = new SMTPAuthenticator();
		Session session = Session.getDefaultInstance(props, auth);
		
		session.setDebug(false);
		
		Message msg = new MimeMessage(session);
		
		
		try {
			msg.setFrom(new InternetAddress(from));//보내는 사람설정.
			msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));//받는사람 설정
			msg.setSubject(subject);
			msg.setContent(content, "text/html; charset=utf-8");
			msg.setSentDate(new Date());
			Transport.send(msg);
			return true;
		} catch (AddressException e) {
			e.printStackTrace();
			return false;
		} catch (MessagingException e) {
			e.printStackTrace();
			return false;
		}//보내는 사람설정
		
	}
	private String makeContent(){
		StringBuffer strStyle = new StringBuffer();
		strStyle.append("<HTML>");
		strStyle.append("<HEAD>");
		strStyle.append("<TITLE>개선제안 진행사항11</TITLE>");
		strStyle.append("<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=euc-kr\">");
		strStyle.append("<style type=\"text/css\">");
		strStyle.append(".style2 {font-size: 12px}");
		strStyle.append(".style3 {color: #FFFFFF}");
		strStyle.append(".style5 {font-size: 10px}");
		strStyle.append(".style7 {color: #FF0066}");
		strStyle.append(".HeadRow {font-size: 12px; color: #576170; height:25px; font-weight:bold; PADDING:4px 2px 1px 2px; BACKGROUND-COLOR:#D7EBF7; text-align:center; border: 1px solid #AFB9C8; white-space:nowrap;}");
		strStyle.append(".ListRowA {font-size: 12px; height:25px; PADDING:4px 3px 1px 3px; BACKGROUND-COLOR:#fff; border: 1px solid #AFB9C8; Line-height:15px;}");
		strStyle.append(".ListRowB {font-size: 12px; height:25px; PADDING:4px 3px 1px 3px; BACKGROUND-COLOR:#F2F2F2; border: 1px solid #AFB9C8; border-collapse: collapse; Line-height:15px;}");
		strStyle.append(".GuideMail	{font-size: 12px; color: #333333; Line-height:16px}");
		strStyle.append("</style>");
		strStyle.append("</HEAD>");
		strStyle.append("<BODY BGCOLOR=#FFFFFF LEFTMARGIN=0 TOPMARGIN=0 MARGINWIDTH=0 MARGINHEIGHT=0>");
		strStyle.append("<TABLE WIDTH=600 BORDER=0 CELLPADDING=0 CELLSPACING=0>");
		strStyle.append("<tr>");
		strStyle.append("<td>");
		strStyle.append("<p class=\"style2\"> 인증 값은"+ranNo+"</p>");
		strStyle.append("</td>");
		strStyle.append("</tr>");
		strStyle.append("</TABLE>");
		strStyle.append("<table cellpadding=\"0\" cellspacing=\"0\">");
		strStyle.append("<tr>");
		strStyle.append("<td width=\"599\"><p class=\"style2\">&nbsp; </p></td>");
		strStyle.append("</tr>");
		strStyle.append("<tr>");
		strStyle.append("<td><p align=\"center\" class=\"style2\">* 문의사항은 전자게시 my SUN Q&amp;A를 이용해 주십시오 * <br>");
		strStyle.append("&lt;문의&gt; 박군 연락처 (Tel: 010-XXXX-XXXX E-mail: <a href=\"mailto:str123.0@gmail.com\">str123.0@gmail.com)</a><br>");
		strStyle.append("</p></td>");
		strStyle.append("</tr>");
		strStyle.append("</table>");
		strStyle.append("<p>&nbsp;</p>");
		strStyle.append("</BODY>");
		strStyle.append("</HTML>");
		
		String content = strStyle.toString();
		return content;
	}
	
	@Override
	public Boolean sendmailtoCliend() {
		System.out.println("메일발송을 시작합니다.");
		String from ="str123.0@gmail.com";//보내는 사람
		String to = email;// 메일 보낼사람?
		String subject ="박군 테스트중...1";//제목
		
		String content = makeContent();
		//메일 발송
		boolean sendResult = sendEmail(from, to, subject, content);
		System.out.println(sendResult);
		
		return sendResult;
	}

}
